#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int N = 1e5 + 10;

int n;
int q[N];

int main(){

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    for(int i = 0; i < n; i ++) cin >> q[i];
    sort(q, q + n);

    LL res = 0;
    LL mid = q[n / 2];
    
    for(int i = 0; i < n; i ++) res += abs(q[i] - mid);
    cout << res << '\n';
    

    return 0;
}